package com.hfi.simplefactory.demo01;

/**
 * @author ChangLiang
 * @date 2019/12/12
 */
public class Main {

    public static void main(String[] args) {
        NoteBookFactory factory = new NoteBookFactory();
        factory.produceNoteBook("lenovo").sayHello();
        factory.produceNoteBook("dell").sayHello();
    }
}

abstract class NoteBook {
    public abstract void sayHello();
}

class Lenovo extends NoteBook {

    @Override
    public void sayHello() {
        System.out.println("lenovo notebook");
    }
}

class Dell extends NoteBook {

    @Override
    public void sayHello() {
        System.out.println("dell notebook");
    }
}

/**
 * 简单工厂优点：工厂类中包含了必要的逻辑判断，根据客户端的选择条件动态实例化相应的类
 * 简单工厂问题：如果要加功能，是一定需要给工厂类的方法里加如'case'的分支条件的 违反了开闭原则 open for extension close for modification
 */
class NoteBookFactory {

    /**
     *
     * @param brand
     * @return 父类 但父类中装的是子类对象
     */
    public NoteBook produceNoteBook(String brand) {
        NoteBook noteBook;
        switch (brand) {
            case "lenovo":
                noteBook = new Lenovo();
                break;
            case "dell":
                noteBook = new Dell();
                break;
            default:
                noteBook = null;
                break;
        }
        return noteBook;
    }
}
